/**
 * Validates the event ticket price.
 * 
 * @author Michael Serajnik
 */

package at.fhj.itm10.ticketmaster.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value = "TicketPriceValidator")
public class TicketPriceValidator implements Validator {

	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		
		String ticketPrice = String.valueOf(value);
		String regEx = "^(?!0*[.,]0*$|[.,]0*$|0*$)\\d+[.,]?\\d{0,2}$";
		
		if(ticketPrice.length() == 0 || ticketPrice.matches(regEx) == false) {
			throw new ValidatorException(new FacesMessage("Bitte gib einen g\u00FCltigen Ticketpreis an."));
		}
		
	}

}